package com.lz.ionicbase.ui.activity;

import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.lz.ionicbase.R;
import com.lz.ionicbase.ui.fragment.AppFragment;
import com.lz.ionicbase.ui.fragment.MessageFragment;
import com.lz.ionicbase.ui.fragment.UserFragment;
import com.lz.ionicbase.ui.fragment.WorkFragment;
import com.lz.ionicbase.view.MyGridAdapter;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.main_bottomSwitcherContainer)
    LinearLayout mainBottomSwitcherContainer;
    private ArrayList<Fragment> fragmentList;
    public MyGridAdapter appGridAdapter;

    @Override
    protected int getActivityLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);
        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(new MessageFragment());
        fragmentList.add(new WorkFragment());
        fragmentList.add(new AppFragment());
        fragmentList.add(new UserFragment());
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initListener() {
        changeUI(0);
        changeFragment(0);
        int childCount = mainBottomSwitcherContainer.getChildCount();
        for (int i = 0; i < childCount; i++) {
            FrameLayout childFragment = (FrameLayout) mainBottomSwitcherContainer.getChildAt(i);
            childFragment.setOnClickListener(this);
        }
    }


    @Override
    public void onClick(View v) {
        //获取孩子节点索引
        int index = mainBottomSwitcherContainer.indexOfChild(v);
        //根据选中索引修改ui
        changeUI(index);
        //根据选中索引,切换fragment
        changeFragment(index);
    }

    private void changeUI(int index) {
        int childCount = mainBottomSwitcherContainer.getChildCount();
        for (int i = 0; i < childCount; i++) {
            FrameLayout childFragment = (FrameLayout) mainBottomSwitcherContainer.getChildAt(i);
            if(i==index){
                setEnable(childFragment,false);
            }else {
                setEnable(childFragment,true);
            }
        }
    }

    private void setEnable(View view, boolean isEnable) {
        view.setEnabled(isEnable);
        //如果view是一个容器,即是viewGroup
        if (view instanceof ViewGroup){
            int childCount = ((ViewGroup) view).getChildCount();
            for (int i=0;i<childCount;i++){
                setEnable(((ViewGroup) view).getChildAt(i),isEnable);
            }
        }
    }

    private void changeFragment(int index) {
        //容器,根据索引替换fragment
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.main_fragmentContainer,fragmentList.get(index),index+"").commit();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void getGridAdapter(MyGridAdapter adapter){
        this.appGridAdapter = adapter;
    }
    @Override
    public void onBackPressed() {
        if (appGridAdapter.getShowDelete() == true) {
            appGridAdapter.setShowDelete(false);
            AppFragment.setShowDelete(false);
        } else {
            finish();
        }
    }
}
